﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Design;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace WenSkin.Controls
{
    [Designer("WenSkin.Design.Designer.WenLableTextBoxDesigner")]
    public class WenLableTextBox : WenControl
    {
        public WenLableTextBox() : base()
        {
            InitializeComponent();
        }
        private void InitializeComponent()
        {
            textBox = new WenTextBox
            {
                Dock = DockStyle.Fill,
            };
            textBox.TextChanged += (s, e) =>
            {
                //值改变事件
                WenLableTextChangeEventArgs wenChoiceTextChangeEventArgs = new WenLableTextChangeEventArgs(beforeText, textBox.Text);
                this.TextBeforeChanged?.Invoke(s, wenChoiceTextChangeEventArgs);
                if (wenChoiceTextChangeEventArgs.State)
                {
                    beforeText = textBox.Text;
                    this.Text = textBox.Text;
                }
                else
                    textBox.Text = beforeText;
            };

            button = new WenButton()
            {
                Size = new Size(20, textBox.Height),
                Dock = DockStyle.Right,
                Image = Properties.Resources.edit,
                ImageSize = new Size(16, 16)
            };
            button.Click += Button_Click;

            label = new Label
            {
                TextAlign = System.Drawing.ContentAlignment.MiddleLeft,
                Dock = DockStyle.Left,
                Size = new Size(0, 0)
            };


            this.Controls.Add(textBox);
            this.Controls.Add(label);
            this.Controls.Add(button);

            this.Size = new Size(200, this.Height);
        }

        private void Button_Click(object sender, EventArgs e)
        {
            this.textBox.Focus();
            WenLableTextBoxButtonEventArgs args = new WenLableTextBoxButtonEventArgs() { Text = this.Text };

            ButtonClick?.Invoke(this, args);

            if (ShowTextBox && args.ShowTextBox)
            {
                WenSkin.Forms.WenForm form = new Forms.WenForm()
                {
                    StartPosition = FormStartPosition.CenterScreen,
                    Size = new Size(500, 350),
                    Text = "文本输入框"
                };

                WenTextBox textBox = new WenTextBox()
                {
                    Text = this.textBox.Text,
                    Dock = DockStyle.Fill,
                    Multiline = true,
                    PasswordChar = this.textBox.PasswordChar,
                    ReadOnly = this.textBox.ReadOnly,
                    ScrollBars = ScrollBars.Both,
                    WordWrap = false
                };

                WenButton button = new WenButton()
                {
                    Text = "确定",
                    Dock = DockStyle.Bottom,
                };
                button.Click += (s, ex) =>
                {
                    this.textBox.Text = textBox.Text;
                    form.Dispose();
                };

                form.Controls.Add(textBox);
                form.Controls.Add(button);
                form.ShowDialog();
            }
        }

        #region 私有属性

        private WenTextBox textBox;
        private WenButton button;
        private Label label;

        private string beforeText;

        private bool password = false;
        #endregion

        #region 委托事件

        [Category("Wen"), Description("按钮点击事件")]
        public event WenChoiceTextBoxButtonEventHandler ButtonClick;

        [Category("Wen"), Description("值改变前事件")]
        public event WenChoiceTextChangeEventHandler TextBeforeChanged;
        #endregion

        #region 公有属性

        public override Font Font { get => base.Font; set { textBox.Font = value; base.Font = value; } }

        [AmbientValue(""), Category("Wen"), Description("标签显示文本内容")]
        public string TextLable
        {
            get => label.Text;
            set
            {
                label.Text = value;
                label.Size = new Size(TextRenderer.MeasureText(TextLable, this.Font).Width, this.Height);
            }
        }

        [DefaultValue(true), AmbientValue(true), Category("Wen"), Description("点击按钮是否弹出输入框")]
        [RefreshProperties(RefreshProperties.Repaint)]
        public bool ShowTextBox { get; set; } = true;

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        [Category("Wen")]
        [Description("标签文本显示")]
        [Browsable(true)]
        [Bindable(true)]
        [EditorBrowsable(EditorBrowsableState.Always)]
        [RefreshProperties(RefreshProperties.Repaint)]
        public new string Text
        {
            get => textBox.Text;
            set
            {
                textBox.Text = value;
                base.Text = value;
                if (beforeText == null)
                    beforeText = value;
            }
        }

        [DefaultValue(false)]
        [RefreshProperties(RefreshProperties.Repaint)]
        [Category("Wen")]
        [Description("是否可输入状态")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public bool ReadOnly { get => textBox.ReadOnly; set => textBox.ReadOnly = value; }

        [DefaultValue(false), AmbientValue(false), Category("Wen"), Description("是否用●掩盖字符")]
        [RefreshProperties(RefreshProperties.Repaint)]
        public bool Password
        {
            get => this.password;
            set
            {
                password = value;
                if (value)
                {
                    textBox.PasswordChar = '●';
                }
                else
                {
                    textBox.PasswordChar = '\0';
                }
            }
        }

        [DefaultValue(true), AmbientValue(true), Category("Wen"), Description("是否显示按钮")]
        public bool ButtonVisible { get => button.Visible; set => button.Visible = value; }

        [Category("Wen"), Description("是否开启下拉搜索框"), DefaultValue(false)]
        public bool StateComDown { get => textBox.StateComDown; set => textBox.StateComDown = value; }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        [Editor("System.Windows.Forms.Design.ListControlStringCollectionEditor", typeof(UITypeEditor))]
        public AutoCompleteStringCollection AutoCompleteCustomSource { get => textBox.AutoCompleteCustomSource; set => textBox.AutoCompleteCustomSource = value; }

        [Category("Wen"), Description("按钮图标"), DefaultValue(false)]
        public Image Image { get => button.Image; set => button.Image = value; }
        #endregion

        #region 重写事件

        //保持控件宽度
        protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified)
        {
            base.SetBoundsCore(x, y, width, textBox.Height, specified);
        }

        #endregion

        public new void Focus()
        {
            textBox.Focus();
        }
    }

}
